import React, { Component, Fragment } from 'react'
import { Route, Redirect, Switch } from 'react-router-dom';
export default class RouteView extends Component {

  render() {
    // 过滤普通路由
    let routelist = this.props.routes.filter(item => {
      return item.path && item.component
    })
    // 过滤重定向路由
    let routerReject = this.props.routes.filter(item => {
      return item.to && item.from
    })
    return (
      <Fragment>
        <Switch>
          {
            routelist.map((item, index) => {
              return (
                <Route key={index} path={item.path} render={(props) => {
                  if (item.children && item.children.length > 0) {
                    return <item.component {...props} routes={item.children}></item.component>
                  } else {
                    return <item.component {...props}></item.component>
                  }
                }}></Route>
              )
            })
          }
          {
            routerReject.map((item, index) => {
              return <Redirect key={index} to={item.to} from={item.from}></Redirect>
            })
          }
        </Switch>
      </Fragment>
    )
  }
}
